<template>
	<view>
		<meetingHead @getMeetings="getMeetings" @getTerm="getTerm" :reciveMeetings="reciveMeetings" @changeSelect="getChangeStatus">
			<!-- @为子组件向父组件传值  ：为父组件向子组件传值-->
		</meetingHead>
		<view class="text-center">
			<view class="cu-bar bg-white">
				<button class="action" @click="jumpWeek(-1)" :disabled="isPrevious">
					<!-- 调用junmWeek方法 -->
					<view>
						<text class="cuIcon-back text-gray"></text>
						上一周
					</view>
				</button>
				<view class="content text-blod">
					第{{meetings.week}}周
				</view>
				<button class="action backButton" @click="jumpWeek(1)" :disabled="isNext">
					<view>
						下一周
						<text class="cuIcon-right text-gray"></text>
					</view>
				</button>

			</view>
			<view class="grid col-2 ">
				<view class="padding cu-form-group Block">
					<view class="title">时间：</view>
					<input name="input" disabled='true' v-model="meetings.date"></input>
				</view>
				<view class="padding cu-form-group">
					<view class="title">地点：</view>
					<input name="input" disabled='true' v-model="meetings.place"></input>
				</view>
				<view class="padding cu-form-group">
					<view class="title">会议主持：</view>
					<input name="input" disabled='true' v-model="meetings.meetingHost"></input>
				</view>
				<view class="padding cu-form-group">
					<view class="title">会议记录：</view>
					<input name="input" disabled='true' v-model="meetings.recorder"></input>
				</view>
			</view>
			<view class="grid col-1 Block">
				<view class="padding cu-form-group">
					<view class="title">请假人员：</view>
					<input name="input" disabled='true' v-model="meetings.leavingPerson"></input>
				</view>
				<view class="padding cu-form-group">
					<view class="title">迟到人员：</view>
					<input name="input" disabled='true' v-model="meetings.latePerson"></input>
				</view>
			</view>

		</view>
		<view class="cu-form-group margin-top Block meetingInfo ">
			<textarea class="text" maxlength="-1" :disabled="modalName!=null" placeholder="会议内容"
				v-model="meetings.content"></textarea>
		</view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				serverUrl: this.$globalUrl, //全局url
				AdmissionTime: this.$AdmissionTime, //全局，开学时间
				nowTerm: this.$globalTerm, //全局，当前学期
				meetings: {
					week: 1
				},
				token: {},
				reciveMeetings: {},
				isLogin: uni.getStorageSync("isLogin"),
        isNext: false,
        isPrevious: false,
        changeStatus: ""
			}
		},

		onLoad: function(options) {
			if (this.isLogin) {
				this.getMeetingInfo()
			} else {
				wx.showToast({
					title: '要先登录噢',
					icon: 'error',
					duration: 2000,
				})
        this.isPrevious = true
        this.isNext = true
			}
		},
		methods: {
			getMeetings(meetings) { //子组件向父组件传来的会议记录信息
				this.meetings = meetings
				console.log(meetings)
			},
			getTerm(term) { //子组件向父组件传来的学期
				this.nowTerm = term
				console.log(this.nowTerm)
			},
			getMeetingInfo() {
				let that = this;
				var td = this.TodayInfo(this.AdmissionTime);
				uni.request({
						url: that.serverUrl + '/meet/getMeetingInfo',
						data: {
							week: td.week-1,
							term: this.nowTerm,
						},
						header: {
							"Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
						}
					}).then(data => {
						var [error, res] = data;
						if (res.data.code == 200) {
							console.log(res.data)
							var meeting = res.data.object;
							this.meetings = meeting[0];
							this.reciveMeetings = this.meetings;
							console.log(uni.getStorageSync('tokenHead') + " " + uni.getStorageSync('token'))
						} else {
							console.log(uni.getStorageSync('tokenHead') + " " + uni.getStorageSync('token'))
							wx.showToast({
								title: '请选择学期鸭~',
								icon: 'error',
								duration: 2000,
							})
              that.isPrevious = true
              that.isNext = true
						}
					}).catch((e) => {
						console.log(uni.getStorageSync('tokenHead') + " " + uni.getStorageSync('token'))
						wx.showToast({
							title: '获取信息失败了',
							icon: 'error',
							duration: 2000,
						})
						console.log(e)
					})
			},

			TodayInfo(start) {
				var WEEKLEN = 7, // 一周7天为常量
					WEEKDAYS = ["日", "一", "二", "三", "四", "五", "六"],
					weekInfo = {
						"week": null,
						"day": null
					}, // 初始化返回信息，默认第null周，星期null
					oneDay = 24 * 60 * 60 * 1000, // 一天的毫秒时长
					weekLeave, // 开学当天所在周剩余天数
					weekStart, // 开学当天start是星期几
					today, // 今天
					dateDiff, // 今天与开学当天日期差
					sDate; //开学之日，日期对象
				var rDateStr = /\d{4}[\/-]\d{1,2}[\/-]\d{1,2}/g; // 简单的日期格式校验：2013/12/19
				if (!rDateStr.test(start)) {
					alert("请使用合法的开学日期！！！");
					return weekInfo;
				}
				sDate = new Date(start.replace("-", "/"));
				weekStart = sDate.getDay();
				weekStart = weekStart === 0 ? 7 : weekStart; // JS中周日的索引为0，这里转换为7，方便计算

				weekLeave = WEEKLEN - weekStart;
				today = new Date();
				weekInfo.day = WEEKDAYS[today.getDay()];
				today = new Date(today.getFullYear() + "/" + (today.getMonth() + 1) + "/" + today.getDate());
				dateDiff = today - sDate;
				if (dateDiff < 0) {
					alert("别开玩笑了，你还没开学呢！！！");
					return weekInfo;
				}
				dateDiff = parseInt(dateDiff / oneDay);
				weekInfo.week = Math.ceil((dateDiff - weekLeave) / WEEKLEN);
				return weekInfo;
			},

			jumpWeek(num) {
				let that = this;
				var week = that.meetings.week + num;
				this.meetings.week = week
        if (that.meetings.week <= 1) {
          that.meetings.week = 1
          that.isPrevious = true
        } else {
          that.isPrevious = false
        }
        
        if (that.meetings.week === 15) {
          that.isNext = true
        } else {
          that.isNext = false
        }
				console.log(this.meetings.week)
        console.log(that.meetings.week)
				uni.request({
						url: that.serverUrl + '/meet/getMeetingInfo',
						data: {
							week: week,
							term: this.nowTerm
						},
						header: {
							"Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
						}
					}).then(data => {
						var [error, res] = data;
						if (res.data.code == 200) {
							var meeting = res.data.object;
							that.meetings = meeting[0];
							this.reciveMeetings = this.meetings;
						} else {
							wx.showToast({
								title: '已经没有记录啦',
								icon: 'none',
								duration: 2000
							})
						}
					})
			},
      getChangeStatus(data) {
        this.changeStatus = data
        console.log(this.changeStatus)
        if (this.changeStatus) {
          this.isPrevious = false
          this.isNext = false
        }
      }
		}
	}
</script>

<style lang="scss">
	.Block {
		border-top: 1rpx solid #eee;
	}

	.meetingInfo {
		height: 40vh;
		width: 100%;

		.text {
			height: 90%;
			font-size: 30rpx;
		}
	}

	.backButton {
		font-size: 30rpx;
	}
</style>
